$(function () {
    $('#Commonbottom').load('../assets/public/html/PublicBottom.html')

    toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }

    function errorWindows(text1, text2) {
        toastr.error(`${text2}`, `${text1}`)
    }

    function successWindows(text1, text2) {
        toastr.success(`${text2}`, `${text1}`)
    }

    //接口数据
    function initialization() {
        let getUserState = Interceptor('/getUserState', 'post')
        return getUserState
    }

    const res = initialization()

    //渲染数据
    function pageCom(res) {
        $('.filing_ulbox').html('')
        //判断内容是否为空
        if (res.data.total != 0) {
            $('.filing_ulbox').removeClass('nullUl')
        } else {
            $('.filing_ulbox').addClass('nullUl').text('暂无内容')
        }
        //将用户投档的数据遍历出来
        for (let i = 0; i < 8; i++) {
            if (res.data.data[i]) {
                const pageCom = `
                        <li class="filing_libox" official_id="${res.data.data[i].official_id}">
                        <div class="essential_information">
                            <h4 class="info_text">${res.data.data[i].official}<span style="color: #FF511C;margin-left: 15px">${res.data.data[i].job_money}</span></h4>
                            <div>
                                <div class="info_condition">
                                    <p style="display: ${res.data.data[i].requirement ? 'dispaly' : 'none'}">${res.data.data[i].requirement}</p>
                                    <p style="display: ${res.data.data[i].education ? 'dispaly' : 'none'}">${res.data.data[i].education}</p>
                                    <img src="${res.data.data[i].header}" alt="">
                                    <span>${res.data.data[i].username}</span>
                                </div>

                                <div class="company_info">
                                    <img src="${res.data.data[i].company_logo}" alt="">
                                    <span>${res.data.data[i].company_name}</span>
                                </div>
                            </div>

                        </div>
                        <div class="input_status">
                            <ul value="${res.data.data[i].state}">
                                <li>退档</li>
                                <li>待审核</li>
                                <li>待面试</li>
                                <li>已面试</li>
                                <li>聘用</li>
                            </ul>
                            <div style="float: right;">
                                <button class="status_combtn return_btn" onclick="bakcBtn($(this))">退档</button>
                                <button class="status_combtn interview_btn" onclick="interviewBtn($(this))">面试</button>
                            </div>
                        </div>
                    </li>`
                $('.filing_ulbox').append(pageCom)


            }
        }
        statusCurrent()
    }

    pageCom(res)
    pagePagination(res)

    //更新状态条的状态
    function statusCurrent() {
        const iptUl = $('.input_status ul');

        for (let i = 0; i < iptUl.length; i++) {
            const value = $(iptUl[i]).attr('value')
            const allBtn = $(iptUl[i]).parent().children('div').children();
            const backBtn = $('.return_btn').eq(i);
            const hirekBtn = $('.interview_btn').eq(i);


            $(iptUl[i]).children().eq(value - 1).addClass('status_current').siblings().removeClass('status_current')

            switch (parseInt(value)) {
                case 1:
                    allBtn.prop('disabled', true).addClass('back')
                    break
                case 2:
                    hirekBtn.prop('disabled', true).addClass('back')
                    break
                case 3:
                    hirekBtn.text('已面试')
                    break
                case 4:
                    hirekBtn.prop('disabled', true).addClass('back')
                    break
                case 5:
                    hirekBtn.text('已聘用').prop('disabled', true).addClass('back')
                    break
            }
        }
    }

    statusCurrent()

    //退档按钮
    bakcBtn = function (dom) {
        const offId = dom.parents('.filing_libox').attr('official_id')
        const upUserState = Interceptor('/upUserState', 'post', {
            'offId': parseInt(offId),
            'state': 1
        })

        const {code, message} = upUserState
        if (code != 666) {
            errorWindows(message, '')
        } else {
            successWindows(message, '');
            pageCom(initialization())
        }
    }

    //面试按钮
    interviewBtn = function (dom) {
        const offId = dom.parents('.filing_libox').attr('official_id')

        const upUserState = Interceptor('/upUserState', 'post', {
            'offId': parseInt(offId),
            'state': 4
        })
        const {code, message} = upUserState
        if (code != 666) {
            errorWindows(message, '')
        } else {
            successWindows(message, '');
            pageCom(initialization())

        }
    }

    //分页
    function pagePagination(res) {
        $('.pageBox').pagination({
            current: 1, //当前页
            showData: 5, //每页显示的数据
            pageCount: res.data.last_page, //总页数
            activeCls: 'page_itemActive',
            callback: function (index) {
                pageIndex = index.getCurrent();
                // pageCom(pageIndex)
                let states = ''
                for (let i = 0; i < $('.filing_status li').length; i++) {
                    if ($($('.filing_status li').eq(i)).children().hasClass('fbtn')) {
                        states = $($('.filing_status li').eq(i)).attr('state')
                    }
                }
                let getUserState = Interceptor('/getUserState', 'post', {
                    'state': states,
                    'currPage': pageIndex
                })

                pageCom(getUserState)
            }
        })
    }

    //热招职位
    function initializationJob() {
        let getHotFiling = Interceptor('/getHotFiling', 'post')

        return getHotFiling
    }

    let getHotFiling = initializationJob();
    jobCom(getHotFiling)


    //切换点击
    $('.filing_status ul li').click(function () {
        const status = $(this).attr('state')
        $(this).children().addClass('fbtn')
        $(this).siblings().children().removeClass('fbtn')
        changeStates(status)
    })

    //切换转台输出内容
    function changeStates(stateId) {
        const getUserState = Interceptor('/getUserState', 'post', {
            'state': stateId
        })

        pageCom(getUserState)
        pagePagination(getUserState)
    }

    //渲染热招职位数据
    function jobCom(res) {
        $('.filing_r ul').html('')
        if (res.data.length != 0) {
            $('.filing_r ul').removeClass('rUl')
        }else {
            $('.filing_r ul').addClass('rUl').text('暂无内容')
        }
        for (let i = 0; i < res.data.length; i++) {
            let company_area = res.data[i].company_area;
            company_area = company_area.slice(3, 5)
            const jobStr = `
                    <li style="margin-top: 15px;">
                       <a href="PositionDetailsPage.html?officialId=${res.data[i].official_id}">
                    <img class="company_logo" src="${res.data[i].company_logo}">
                    <div>
                        <p>${res.data[i].official}</p>
                        <p>${res.data[i].company_name}</p>
                    </div>

                    <div>
                        <p><img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/right_icon-location.png" alt="" class="va">
                        <span class="area">${company_area}</span><span class="condition">${res.data[i].range_money}</span></p>
                        <p>${res.data[i].education}</p>
                    </div>
                </a></li>
            `
            $('.filing_r ul').append(jobStr)
        }
    }

    //更换热招职位数据
    $('.change').click(function () {

        function initializationJob() {
            let getHotFiling = Interceptor('/getHotFiling', 'post')
            return getHotFiling
        }

        let getHotFiling = initializationJob();

        jobCom(getHotFiling)
    })
})